package com.suixun.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suixun.dao.CooperationPolicyMapper;
import com.suixun.dao.redis.RedisDao;
import com.suixun.domain.CooperationPolicy;
import com.suixun.exceptions.SuixunException;
import com.suixun.service.CooperationPolicyService;
import com.suixun.utils.ListUtil;

/**
 * <p>
 * 合作政策 服务实现类
 * </p>
 *
 * @author jacky
 * @since 2018-01-11
 */
@Service
public class CooperationPolicyServiceImpl extends ServiceImpl<CooperationPolicyMapper, CooperationPolicy>
        implements CooperationPolicyService {

    @Value("${cooperation.policy.num}")
    private Integer displayNum; // 前台展示数量
    @Resource
    private CooperationPolicyMapper cooperationPolicyMapper;
    @Resource
    private RedisDao redisDao;

    @Override
    @Cacheable(value = "cooperationPolicyCache", key = "'cooperationPolicy_listCooperationPolicyExclusionContent'")
    public List<CooperationPolicy> listCooperationPolicyExclusionContent() {
        Map<String, Object> map = new HashMap<>();
        map.put("displayNum", displayNum);
        return cooperationPolicyMapper.listCooperationPolicyExclusionContent(map);
    }

    @Override
    @Cacheable(value = "cooperationPolicyCache", key = "'cooperationPolicy_getById_' + #id")
    public CooperationPolicy getById(Integer id) {
        return this.selectById(id);
    }

    @Override
    public void insert(CooperationPolicy cooperationPolicy, Boolean isTop) {
        if (isTop != null && isTop) {
            cooperationPolicy.setTop(String.valueOf(new Date().getTime()));
        }
        this.insert(cooperationPolicy);
        // 清缓存
        redisDao.flushCaches("cooperationPolicy*");
    }

    @Override
    public void update(CooperationPolicy cooperationPolicy, Boolean isTop) {
        if (cooperationPolicy.getId() == null) {
            throw new SuixunException("id不能为空");
        }
        Date date = new Date();
        if (isTop != null) {
            if (isTop) {
                cooperationPolicy.setTop(String.valueOf(date.getTime()));
            } else {
                cooperationPolicy.setTop("0");
            }

        }

        this.updateById(cooperationPolicy);
        // 清缓存
        redisDao.flushCaches("cooperationPolicy*");
    }

    @Override
    public void delete(String ids) {

        this.deleteBatchIds(ListUtil.Integers2List(ids));
        // 清缓存
        redisDao.flushCaches("cooperationPolicy*");
    }

    @Override
    public PageInfo<CooperationPolicy> pageExclusionContent(int page, int size, String title, Integer isShow) {
        PageHelper.startPage(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("isShow", isShow);
        return new PageInfo<>(cooperationPolicyMapper.listCooperationPolicyExclusionContent(map));
    }

}
